/* eslint-disable no-empty */
<template>
  <a-modal v-model="isVisible"
           :title="title"
           ok-text="同步"
           cancel-text="取消"
           @cancel="cancel"
           @ok="sync">
    <a-row>
      <a-col :span="24">
        <a-form-model ref="form"
                      :model="form"
                      :label-col="{ span: 6 }"
                      :wrapper-col="{ span: 18 }">
          <a-form-model-item :label="name"
                             prop="name">
            <span>{{ form.name }}</span>
          </a-form-model-item>
          <a-form-model-item v-if="mediaType === '1' && syncType === '4'"
                             label="同步子级机构"
                             prop="isChildUnit">
            <z-switch-one-zero checked-children="是"
                               un-checked-children="否"
                               default-checked
                               v-model="form.isChildUnit"></z-switch-one-zero>
          </a-form-model-item>
          <a-form-model-item v-if="mediaType === '1' && syncType === '4' "
                             label="同步子级用户"
                             prop="isChildUser">

            <z-switch-one-zero checked-children="是"
                               un-checked-children="否"
                               default-checked
                               v-model="form.isChildUser"></z-switch-one-zero>
          </a-form-model-item>
        </a-form-model>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
import { getById as getUnit } from '../../api/unit'
import { getById as getUser } from '../../api/user'
import { sync } from '../../api/application/sync'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
      description: '是否显示弹框'
    },
    mediaType: {
      type: String,
      required: true,
      description: '1机构2用户'
    },
    mediaId: {
      type: String,
      required: true,
      description: '机构、用户'
    },
    deptId: {
      type: String,
      required: false,
      description: '用户所在部门id',
      default: ''
    },
    syncType: {
      type: String,
      required: true,
      description: '1新增2编辑3删除4批量'
    }
  },
  data () {
    return {
      isVisible: false,
      title: '',
      name: '',
      form: {
        name: '',
        isChildUnit: '',
        isChildUser: ''
      }
    }
  },
  methods: {
    init () {
      // eslint-disable-next-line no-empty
      if (this.mediaType === '1') {
        getUnit(this.mediaId).then(res => {
          this.form.name = res.name
        })
        // eslint-disable-next-line no-empty
      } else if (this.mediaType === '2') {
        getUser(this.mediaId).then(res => {
          this.form.name = res.userVO.fullname
        })
      }
    },
    sync () {
      const _this = this
      const param = {
        mediaId: this.mediaId,
        deptId: this.deptId,
        mediaType: this.mediaType,
        syncType: this.syncType,
        isChildUnit: this.form.isChildUnit,
        isChildUser: this.form.isChildUser
      }
        sync(param).then(res => {
          // if (res.length > 0) {
          //   res.forEach(item => {
          //     _this.message.info(item.data)
          //   })
          // }
          _this.message.success('同步成功')
          _this.isVisible = false
          _this.cancel()
        })
    },
    cancel () {
        this.$emit('cancel')
    }
  },
  watch: {
    visible: {
      handler: function () {
        this.isVisible = this.visible
        this.form.isChildUnit = ''
        this.form.isChildUser = ''
        // eslint-disable-next-line no-empty
        if (this.mediaType === '1') {
          this.title = '机构同步'
          this.name = '机构名称'
          // eslint-disable-next-line no-empty
        } else if (this.mediaType === '2') {
          this.title = '用户同步'
          this.name = '用户名称'
        }
        if (this.isVisible === true) {
          this.init()
        }
      }
    }
  }
}
</script>

<style>
</style>
